﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace CollisionDetectionSandbox
{
    public class HealthUp
    {
        public Vector2 Location { get; set; }
        public Vector2 Size { get { return SIZE; } }

        public float Left { get { return Location.X - (Size.X / 2f); } }
        public float Right { get { return Location.X + (Size.X / 2f); } }
        public float Top { get { return Location.Y - (Size.Y / 2f); } }
        public float Bottom { get { return Location.Y + (Size.Y / 2f); } }

        private static readonly Vector2 SIZE = new Vector2(20, 20);
        private const float HEALTH_SPEED = 1f;//pixels per update

        public static List<HealthUp> ObjectList = new List<HealthUp>();

        public HealthUp(Vector2 location)
        {
            Location = location;
        }

        public void MoveOneStep()
        {
            //get unit vector in direction of enemy travel
            Vector2 unit = new Vector2(0, -1 * HEALTH_SPEED);

            //add to current location
            Location = new Vector2(Location.X + unit.X, Location.Y - unit.Y);
        }

        public bool IsHittingBottom()
        {
            bool bHitting = (this.Bottom >= GameScreen.PlayerLocation.Y);

            return bHitting;
        }

        public void Delete()
        {
            ObjectList.Remove(this);
        }

        public bool IsHittingPlayer()
        {
            bool bHitting = false;

            BoundingSphere sphereHead = new BoundingSphere(new Vector3(GameScreen.PlayerLocation, 0), 25f);
            BoundingSphere sphereHand = new BoundingSphere(new Vector3(GameScreen.HandLocation, 0), 15);
            BoundingSphere sphereItem = new BoundingSphere(new Vector3(this.Location, 0), 10);

            if (sphereItem.Intersects(sphereHead) || sphereItem.Intersects(sphereHand))
            {
                bHitting = true;
                GameScreen.powerUp.Play();
            }

            return bHitting;
        }

        public void Draw(SpriteBatch batch)
        {
            //it wants the top left corner for location, not the center
            Vector2 drawLoc = new Vector2(Left, Top);
            batch.Draw(GameScreen.HealthUpTexture, drawLoc, Color.White);
        }
    }
}
